package org.hackystat.devcathlon.ui.page.scoreboard.teamrightpanel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.hackystat.devcathlon.engine.match.MatchManager;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.engine.user.User;
import org.hackystat.devcathlon.ui.DevcathlonSession;
import org.hackystat.devcathlon.ui.page.profile.ProfilePage;

/**
 * The right panel holds team lineup.
 * 
 * @author Daniel Arakaki
 *
 */
public class FrontTeamRightPanel extends Panel implements Serializable {

  /** Support serialization. */
  private static final long serialVersionUID = 1L;

  /**
   * Initialize sessions.
   * 
   * @param wicketId The id from the scoreboardpage.
   * @param matchNames A String list of matchNames.
   */
  public FrontTeamRightPanel(String wicketId, final List<String> matchNames) { 
    super(wicketId);
    
    String noMatch = "";
    if (matchNames.isEmpty()) {
      noMatch = "No teams...";
      
      List<User> memberList = new ArrayList<User>();

      add(new Label("teamRightHeader", ""));
      
      DataView dataview = new DataView("ItemList", new ListDataProvider(memberList)) {

        /** Support serialization. */
        private static final long serialVersionUID = 1L;

        @Override
        protected void populateItem(Item item) {
          final User userItem = (User) item.getModelObject();
          String userName = userItem.getFirstName().concat(" " + userItem.getLastName());
          final String userEmail = userItem.getEmail();
          Integer point = 
            MatchManager.getInstance().getMatch(matchNames.get(0)).getTotalScore(userItem);
          
          Link userProfileLink = new Link("userProfileLink", item.getModel()) {
            /** Support serialization. */
            private static final long serialVersionUID = 1L;
            
            @Override
            public void onClick() {
              DevcathlonSession.get().getProfileSession().setProfile(userEmail);
              setResponsePage(new ProfilePage());
            }
          };
          userProfileLink.add(new Label("frontteamrightpanel", userName));
          item.add(userProfileLink);
          item.add(new Label("playerpoints", point.toString()));
        }
      };
      add(dataview);
    }
    else {
      Integer point;
      
      List<Team> teamList = 
        new ArrayList<Team>(MatchManager.getInstance().getMatch(matchNames.get(0)).getTeams());
      
      List<User> userList = new ArrayList<User>(teamList.get(1).getMembers());
      //userList.add(TeamManager.getInstance().getTeam(teamList.get(1).getName()).getOwner());
      add(new Label("teamRightHeader", teamList.get(1).getName()));
      
      final Map<User, Integer> scoreMap = new HashMap<User, Integer>();
      
      for (User userData : userList) {
        point = MatchManager.getInstance().getMatch(matchNames.get(0)).getUserPoints(userData);
        scoreMap.put(userData, point);
      }
      
      DataView dataview = new DataView("ItemList", new ListDataProvider(userList)) {

        /** Support serialization. */
        private static final long serialVersionUID = 1L;

        @Override
        protected void populateItem(Item item) {
          final User userItem = (User) item.getModelObject();
          String userName = userItem.getFirstName().concat(" " + userItem.getLastName());
          final String userEmail = userItem.getEmail();
          
          Link userProfileLink = new Link("userProfileLink", item.getModel()) {
            /** Support serialization. */
            private static final long serialVersionUID = 1L;
            
            @Override
            public void onClick() {
              DevcathlonSession.get().getProfileSession().setProfile(userEmail);
              setResponsePage(new ProfilePage());
            }
          };
          userProfileLink.add(new Label("frontteamrightpanel", userName));
          item.add(userProfileLink);
          item.add(new Label("playerpoints", scoreMap.get(userItem).toString()));
        }
      };
      add(dataview);
    }
    add(new Label("noMatch", noMatch));
  }
}
